// check if scrolling has taken place, using the didScroll function, and update positions of scrollable elements if needed
if (didScroll())
{
updateScrollPositions()
}
}
catch(e)
{
// if didScroll function is not present, ignore error
}
}
function findClosestItem(direction)
{
/* this function finds the the best item to navigate to based on which arrow key is pressed and on which item currently has the focus. To do this, it does up to three loops through all of the focusable items. On the first loop, it looks for items that are in a direct line in the direction selected, and finds the closest one. If the first loop finds no results, the second loop widens the search, looking for the closest item in the quadrant of the page (dividing the page like an X, not like a cross, with the current focus item at the center) in the direction selected. If there are no focusable items in the correct quadrant, the search widens a bit more, finding the closest item anywhere on the page (or, in effect, half of the page) in the direction selected. */
//Variable for object that currently has focus
var oOldObj = oCurFocus;
// variable to track shortest item distance. Start with a high number (10,000) for value
var nShortestItemDist = 10000
// variable to track which object is nearest to the one that currently has focus
var oNearestObj = null
// first try to find any items that are exactly in the direction indicated; if any, return closest item
for (i=0; i < aFocusableItemsArray.length; i++)
{
// Locate new object in array
oNewObj = aFocusableItemsArray[i]
// make sure object is not temporarily unfocusable
if (oNewObj.MCTempUnFocusable == "true") continue
// Make sure oNewObj is not oOldObj
if (oOldObj == oNewObj)
{
continue
}
// call function to check if new object is in a straight line from old object, in direction indicated by selected arrow key;
// if so, set nTempDist variable for distance
var nTempDist = isInLine(oOldObj, oNewObj, direction)
// if current object's distance is closest so far ...
if (nTempDist != null && nTempDist < nShortestItemDist)
{
// update variable for shortest distance so far
nShortestItemDist = nTempDist
// update variable for closest object so far
oNearestObj = oNewObj
}
}
// if above process finds any objects in a straight line from old object, return the closest one.
if (oNearestObj != null)
{
return oNearestObj
}
////////////////////////////
/* If no items exactly in correct direction, try to find any items that are in the correct quadrant
for the direction indicated; if any, return closest item */
for (i=0; i < aFocusableItemsArray.length; i++)
{
// Locate new object in array
oNewObj = aFocusableItemsArray[i]
// make sure object is not temporarily unfocusable
if (oNewObj.MCTempUnFocusable == "true") continue
// Make sure oNewObj is not oOldObj
if (oOldObj == oNewObj) continue
// call function to check if new object is in the correct quadrant from old object, in direction indicated by selected arrow key;
// if so, set nTempDist variable for distance
var nTempDist = isInQuadrant(oOldObj, oNewObj, direction)
// if current object's distance is closest so far (of items in this loop) ...
if (nTempDist != null && nTempDist < nShortestItemDist)
{
// update variable for shortest distance so far
nShortestItemDist = nTempDist
// update variable for closest object so far
oNearestObj = oNewObj
}
}
// if above process finds any objects in the correct quadrant from old object, return closest one
if (oNearestObj != null)
{
return oNearestObj
}
/////////////////////////////////////
/* If no items are in correct quadrant, try to find any items that are in the correct half of the page
for the direction indicated; if any, return closest item */
for (i=0; i < aFocusableItemsArray.length; i++)
{
// Locate new object in array
oNewObj= aFocusableItemsArray[i]
// make sure object is not temporarily unfocusable
if (oNewObj.MCTempUnFocusable == "true") continue
// Make sure nNewObj is not oOldObj
if (oOldObj == oNewObj) continue
// call function to check if new object is in the correct quadrant from old object, in direction indicated by selected arrow key;
// if so, set nTempDist variable for distance
var nTempDist = isInHalf(oOldObj, oNewObj, direction)
// if current object's distance is closest so far (in this loop) ...
if (nTempDist != null && nTempDist < nShortestItemDist)
{
// update variable for shortest distance so far
nShortestItemDist = nTempDist
// update variable for closest object so far
oNearestObj = oNewObj
}
}
// if above process finds any objects in the correct quadrant from old object, return closest object
if (oNearestObj != null)
{
return oNearestObj
}
// If not, return null
return null
}
function isInLine(oOldObj, oNewObj, direction)
{
var nStraightDist = null
// make sure object is not temporarily unfocusable
if (direction == "down")
{
// if old object is above (less in y coordinate) new object, return null
if (oOldObj.nCenterYCoord > oNewObj.nCenterYCoord)
{
return null
}
// if the current focus object's left edge is to the left of the new object's right edge, and the
// current focus object's right edge is to the right of the new object's left edge ...
if (oOldObj.nLeftPos < oNewObj.nRightPos && oNewObj.nLeftPos < oOldObj.nRightPos)
{
// then the new object and the old object overlap in the X coordinate
// calculate distance based on (top of oNewObj - bottom of oOldObj)